﻿#include "page1widget.h"
#include "socket.h"
#include "cppfont.h"
#include "json11.hpp"

Page1Widget::Page1Widget() {
    page = 1;
}

void Page1Widget::show() {
    while(1) {
        pageTurnView();
        showPageNum(page);
        showCommodityList(page);

        int x, y;
        int pos = -1;
        //等待触摸屏 触摸
        TouchSreen touch;
        touch.waitTouch(x, y);

        // 退出按钮，跳出当前商品页面，回到main widget
        if(x >= 0 && x < 120 && y >= 360 && y < 480) {
            CppFont::clearArea(120, 0, 680, 480, 0x00ffffff);  // 清空
            cout << "退出商品页面" << endl;
            break;
        }

        if(x >= 120 && x < 800 && y >= 0 && y < 480) { //商品界面
            if(x >= 200 && x < 320 && y >= 50 && y < 170) { // 第一个商品
                pos = 1;
            }
            if(x >= 400 && x < 520 && y >= 50 && y < 170) {
                pos = 2;
            }
            if(x >= 600 && x < 720 && y >= 50 && y < 170) {
                pos = 3;
            }
            if(x >= 200 && x < 320 && y >= 210 && y < 330) {
                pos = 4;
            }
            if(x >= 400 && x < 520 && y >= 210 && y < 330) {
                pos = 5;
            }
            if(x >= 600 && x < 720 && y >= 210 && y < 330) {
                pos = 6;
            }
        }
        // 显示商品具体信息
        if(pos != -1 && pos <= (int)bmpArray.size()) {
            showCommodity(pos);
        }

        // 上一页
        if(x >= 330 && x < 410 && y >= 390 && y < 470 && page > 1) {
            showCommodityList(page - 1);
            showPageNum(page);
        }
        // 下一页
        if(x >= 510 && x < 590 && y >= 390 && y < 470) {
            showCommodityList(page + 1);
            showPageNum(page);
        }
    }
}

void Page1Widget::getCommodityList(int page) {
    string buf = "{\"type\":2,\"page\":" + to_string(page) + "}";
    cout << "buf=" << buf << endl;
    Socket::getSocket()->sendMsg(buf);
    string result;
    Socket::getSocket()->readMsg(result);
    //cout << "result=" << result << endl;

    string err;
    auto json = Json::parse(result, err);
    // json格式正确&&能查到数据
    if(!err.empty() || json["result"].array_items().empty()) {
        cout << "获取数据失败" << endl;
        return ;
    }
    bmpArray.clear();  // 先清空
    this->page = page;  // 获得数据再更新页数
    for (auto &k : json["result"].array_items() ) {
        commodity c;
        c.id = atoi(k["commodity_id"].string_value().c_str());
        c.name = k["name"].string_value();
        c.picture = k["picture"].string_value();
        c.price = k["output_price"].string_value();
        c.amount = k["amount"].string_value();
        //std::cout << c.id << "\t" << c.name << "\t" << c.picture << "\t" << c.price << "\n";
        bmpArray.push_back(c);
    }

}

void Page1Widget::showCommodityList(int page) {
    getCommodityList(page);

    CppFont::clearArea(120, 0, 680, 380, 0x00ffffff);
    // 2行3列
    for(int i = 0; i < (int)bmpArray.size(); i++) {
        Bmp b1(bmpArray[i].picture.c_str());
        if(i < 3) {
            b1.show(200 + i * 200, 50);
        } else {
            b1.show(200 + (i - 3) * 200, 210);
        }
    }

}

void Page1Widget::pageTurnView() {
    Bmp b1;
    b1.setFileName("./res/last_page.bmp");  // 80*80
    b1.show(330, 390);
    b1.setFileName("./res/next_page.bmp");  // 80*80
    b1.show(510, 390);
}

void Page1Widget::showPageNum(int page) {
    CppFont::clearArea(420, 390, 80, 80, 0x00ffffff);
    CppFont::displayCharacter(425, 395, (unsigned char *)to_string(page).c_str(), 0x00000000, 3);
}

void Page1Widget::showCommodity(int pos) {
    commodity cCom = bmpArray[pos - 1];  // 当前商品信息
    CppFont::clearArea(120, 0, 680, 480, 0x00ffffff);  // 清空
    int num = 0; // 当前商品在订单中的数量
    int currentAmount = atoi(cCom.amount.c_str()); // 当前商品库存
    int itemPos = -1;  // 该商品在订单项的位置
    // 查找当前订单中有没有该商品
    for(int i = 0; i < (int)order.size(); i++) {
        if(order[i]->id == cCom.id) {
            num = order[i]->num;
            itemPos = i;
        }
    }

    cout << "itemPos:" << itemPos << " num:" << num << endl;

    Bmp b1;
    b1.setFileName("./res/confirm.bmp");  // 确认 100*100
    b1.show(320, 380);
    b1.setFileName("./res/close.bmp");    // 关闭 100*100
    b1.show(500, 380);
    b1.setFileName("./res/plus.bmp");     // 加 80*80
    b1.show(600, 300);
    // 管理员才显示
    if(currentMember.level == "0") {
        b1.show(600, 200);
    }
    b1.setFileName("./res/subtract.bmp"); // 减 80*80
    b1.show(500, 300);
    if(currentMember.level == "0") {
        b1.show(500, 200);
    }

    string buf;
    buf = "name:";
    buf.append(cCom.name.c_str());
    cout << buf << endl;
    CppFont::displayCharacter(200, 0, (unsigned char *)buf.c_str(), 0x00000000, 2);

    buf = "price:";
    buf.append(cCom.price.c_str());
    cout << buf << endl;
    CppFont::displayCharacter(200, 100, (unsigned char *)buf.c_str(), 0x00000000, 2);

    showCommodityAmout(currentAmount);

    showCommodityNum(num);

    while(1) {
        int x, y;
        //等待触摸屏 触摸
        TouchSreen touch;
        touch.waitTouch(x, y);

        // 等级为0(管理员)才能设置库存
        if(currentMember.level == "0" && x >= 500 && x < 580 && y >= 200 && y < 280 && currentAmount > 0) {
            currentAmount = currentAmount - 1;
            showCommodityAmout(currentAmount);
        }

        if(currentMember.level == "0" && x >= 600 && x < 680 && y >= 200 && y < 280) {
            currentAmount = currentAmount + 1;
            showCommodityAmout(currentAmount);
        }

        if(x >= 500 && x < 580 && y >= 300 && y < 380 && num > 0) {
            num = num - 1;
            showCommodityNum(num);
        }

        if(x >= 600 && x < 680 && y >= 300 && y < 380) {
            num = num + 1;
            showCommodityNum(num);
        }

        // 确认键
        if(x >= 320 && x < 420 && y >= 380 && y < 480) {
            if(itemPos != -1) { // 订单中已经存在该订单项
                order[itemPos]->num = num;
            } else if(num != 0) {  // 订单项不存在且添加的数量不为0
                orderItem *oi = new orderItem;
                oi->id = cCom.id;
                oi->name = cCom.name;
                oi->price = cCom.price;
                oi->num = num;
                order.push_back(oi);
            }

            // 库存发生变化
            if(currentMember.level == "0" && currentAmount != atoi(cCom.amount.c_str())) {
                string json;
                json.append("{\"type\":6,\"id\":\""
                            + to_string(cCom.id)
                            + "\",\"amount\":\"" + to_string(currentAmount)
                            + "\"}");
                cout << "json:" << json << endl;
                Socket::getSocket()->sendMsg(json);
            }

            cout << "==order==" << endl;
            for (int j = 0; j < (int)order.size(); j++) {
                cout << order[j]->id << " " << order[j]->name
                     << " " << order[j]->num << endl;;
            }
            cout << "==order==" << endl;
            break;
        }

        // 关闭键
        if(x >= 500 && x < 600 && y >= 380 && y < 480) {
            break;
        }
    }

}

void Page1Widget::showCommodityNum(int num) {
    CppFont::clearArea(200, 300, 200, 100, 0x00ffffff);
    string buf = "num:";
    buf.append(to_string(num));
    CppFont::displayCharacter(200, 300, (unsigned char *)buf.c_str(), 0x00000000, 2);
}

void Page1Widget::showCommodityAmout(int amount) {
    CppFont::clearArea(200, 200, 200, 100, 0x00ffffff);
    string buf;
    buf = "amount:";
    buf.append(to_string(amount));
    CppFont::displayCharacter(200, 200, (unsigned char *)buf.c_str(), 0x00000000, 2);
}
